package com.enbatis.mybatisplugs.commons.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class LineHumpUtil {

    private static Pattern linePattern = Pattern.compile("_(\\w)");

    /**
     *  下划线转驼峰
     * @param str 下划线字符串
     * @return 驼峰字符串
     */
    public static String lineToHump(String str) {
        str = str.toLowerCase();
        Matcher matcher = linePattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }


    /**
     * 下划线转首字府大写驼峰
     * @param str 下划线str
     * @return 首字符大写驼峰
     */
    public static String lineToBigHump(String str) {
        str = str.toLowerCase();
        Matcher matcher = linePattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString().substring(0, 1).toUpperCase() + sb.toString().substring(1);
    }

    private static Pattern humpPattern = Pattern.compile("[A-Z]");


    /**
     * 驼峰转下划线,效率比上面高
     * @param str 驼峰
     * @return 下划线
     */
    public static String humpToLine(String str) {
        Matcher matcher = humpPattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);

        String returnValue=sb.toString();
        if (returnValue.charAt(0)=='_'){
            returnValue=returnValue.substring(1) ;
        }
        return returnValue;
    }


}
